function _getByDomName(el, domName, bNumber = false) {
    switch (domName) {
        case 'input':
        case 'select':
            if (bNumber) {
                if (el.value.trim().length < 1) {
                    el.value = '0';
                    return 0;
                } else {
                    return Number(el.value);
                }
            } else {
                return el.value;
            }
        case 'checkbox':
            return el.checked;
        default:
            throw new Error(`unknow domName:${domName}`);
    }
}

function _setByDomName(el, domName, value) {
    switch (domName) {
        case 'input':
        case 'select':
            el.value = value;
            break;
        case 'checkbox':
            el.checked = !!value;
            break;
        default:
            throw new Error(`unknow domName:${domName}`);
    }
}

// 实现一个双向绑定
/**
 * item:{
 *     xx:{
 *         name: // 需要绑定的属性名,
 *         el: // dom元素
 *         domName: // dom的名字
 *         bNumber: // 是否数字类型
 *     }
 * }
 * @param model
 * @param item
 */
function doubleBind(model,item) {
    let domName = item.domName;
    let el = item.el;
    let bNumber = item.bNumber || false;
    let name = item.name;

    let callback = item.callback; // model->dom  后面的回调
    Object.defineProperty(model,name, {
        get() {
            return _getByDomName(el,domName,bNumber);
        },
        set(v) {
            // TODO: 绑定log
            // console.log(`dbbind set v:${v}`);
            _setByDomName(el, domName, v);

            /**
             * 注意:
             *  callback 里面,不能去调用 model[name] 的setter,否则会引起死循环
             */
            if(callback){
                callback.call(item);
            }
        },
        enumerable:true,
        configurable:true
    });

    // 事件绑定
    // let domEvent = item.domEvent;
    let filter = item.filter;

    function onEvent(){
        if(filter){
            if(!item.filter()){
                return;
            }
        }
        onInputOrSelectChange();
    }

    function onInputOrSelectChange() {
        console.log(`domName:${domName} event!`);
        let value = el.value.trim();
        if(bNumber && value.length < 1){
            model[name] = 0;
        }else if(bNumber){
            model[name] = Number(value);
        }else{
            model[name] = value;
        }
    }

    switch (domName) {
        case "input":
        case "select":
            el.addEventListener('change',onEvent,false);
            el.addEventListener('blur',onEvent,false);
            break;
        case "checkbox":
            console.log(`domName:${domName} event!`);
            el.addEventListener('change',()=>{
                if(filter){
                    if(!item.filter()){
                        return;
                    }
                }
                model[name] = el.checked;
            },false);
            break;
        default:
            throw new Error(`unknow domName:${domName}`);
    }
}

module.exports = doubleBind;
